<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * New_session Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Sessions
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/sessions.html
 */
class CI_New_Session {

        var $prefix = '';
	var $userdata					= array();
        var $CI;

	/**
	 * Session Constructor
	 *
	 * The constructor runs the session routines automatically
	 * whenever the class is instantiated.
	 */
	public function __construct()
	{
		log_message('debug', "New Session Class Initialized");
                // Set the super object to a local variable for use throughout the class
		$this->CI =& get_instance();
                $this->userdata = null;
                $this->CI->load->library('config');
                $this->prefix = $this->CI->config->item('session_prefix');
		log_message('debug', "New session routines successfully run");
	}

	// --------------------------------------------------------------------
        /**
	 * Sets a session.
	 *
	 * @see Phpfox::setCookie()
	 * @param string $sName Name of the session.
	 * @param string $sValue Value of the session.
	 */	
        public function set($sName, $val)
        {
            if(empty($sName) || !is_string($sName))
            {
                log_message('debug', "Name session is empty.");
                return false;
            }
            
            $_SESSION[$this->prefix . $sName] = $val;
            $this->userdata = $val;
            
            return true;
        }
        
        public function sets(array $mArray){
            if(is_null($mArray) || !is_array($mArray))
            {
                log_message('debug', "Argument supply that is not array type.");
                return false;
            }
            
            if((bool)array_key_exists('', $mArray))
            {
               log_message('debug', "Key of array is not allowed to be empty.");
               return false;
            }
            
            //If having trouble with set all key.
            $isComplete = true;
            foreach($mArray as $sKey => $mVal){
                if(is_string($sKey)){
                    $_SESSION[$sKey] = $mVal;
                }
                else{
                    $isComplete = false;
                    break;
                }
            }
            
            if(!$isComplete){
                foreach ($mArray as $sKey){
                    if(is_string($sKey)){
                        unset($_SESSION[$sKey]);
                    }
                    else{
                        return false;
                    }
                }
            }
            
            return true;
        }
        
        /**
	 * Gets a session.
	 *
	 * @param string $sName Name of the session.
	 * @return mixed Session exists we return its value, otherwise we return FALSE.
	 */
        public function get($sName)
        {
            if(isset($_SESSION[$sName]))
            {
                return empty($_SESSION[$sName]) ? false : $_SESSION[$sName];
            }
            
            return false;
        }
        
        public function getAll()
        {
            if(isset($_SESSION))
            {
                return $_SESSION;
            }
        }
        
        /**
	 * Removes a session.
	 *
	 * @param mixed $mName STRING name of session, ARRAY of sessions.
	 */	
        public function remove($mName = NULL)
        {
            //if $mName is NULL, remove all session
            if(is_null($mName))
            {
                session_destroy();
            }
            //if $mName is string, remove session belong to string
            if(!is_array($mName))
            {
                $mName = array($mName);
            }
            //if $mName is array, remove all session belong to respective array.
            if(is_array($mName))
            {
                foreach ($mName as $sKey)
                {
                    unset($_SESSION[$sKey]);
                }
            }
            
            return true;
        }
}
// END New Session Class

/* End of file New_session.php */
/* Location: ./system/libraries/New_session.php */